package cn.mandifal.user.controller;

import cn.mandifal.pojo.DpAddress;
import cn.mandifal.pojo.DpAreas;
import cn.mandifal.pojo.DpCities;
import cn.mandifal.pojo.DpProvinces;
import cn.mandifal.user.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户地址管理控制层
 * @author shun
 */
@RestController
@RequestMapping("/addressController")
public class AddressController {
    @Reference
    private AddressService addressService;

    /**
     * 查询一个地址信息详情
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public DpAddress findOne(Long id){
        try {
            DpAddress dpAddress = addressService.findOne(id);
            return dpAddress;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 添加地址
     * @param dpAddress
     * @return
     */
    @RequestMapping("/add")
    public Result add( @RequestBody DpAddress dpAddress){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        dpAddress.setUserId(username);
        try {
            addressService.add(dpAddress);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }

    /**
     * 更新地址信息
     * @param dpAddress
     * @return
     */
    @RequestMapping("/update")
    public Result update( @RequestBody DpAddress dpAddress){
        try {
            addressService.update(dpAddress);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }
    /**
     * 删除地址信息
     */
    @RequestMapping("/delete")
    public Result delete(Long id){
        try {
            addressService.delete(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }


    /**
     * 查询所有地址信息
     */
    @RequestMapping("/findAll")
    public List<DpAddress> findAll(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        try {
            List<DpAddress> listByUserId = addressService.findListByUserId(username);
            return listByUserId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有省级地址
     * @return 省名称列表
     */
    @RequestMapping("/findProvinces")
    public List<DpProvinces> findProvinces(){
        return addressService.findProvinces();
    }

    /**
     * 根据省级 id查询市
     * @param provinceId
     * @return
     */
    @RequestMapping("/findCity")
    public List<DpCities> findCity(String provinceId){
        if (provinceId==null){
            return null;
        }
        return addressService.findCity(provinceId);
    }

    /**
     * 根据市级 id 查询县
     * @param cityId
     * @return
     */
    @RequestMapping("/findArea")
    public List<DpAreas> findArea(String cityId){
        if (cityId==null){
            return null;
        }
        return addressService.findArea(cityId);
    }

    /**
     * 设置默认地址
     * @param id
     * @return
     */
    @RequestMapping("/isDefault")
    public Result isDefault(Long id){
        //得到用户名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        try {
            addressService.isDefault(id,name);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }
}
